/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
// 二分查找的方法
var searchInsert = function (nums, target) {
  let left = 0;
  let right = nums.length - 1;

  while (left <= right) {
    const mid = Math.floor((left + right) / 2);

    if (nums[mid] === target) {
      return mid;
    } else if (nums[mid] > target) {
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }

  return left;
};

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
// 最简单的解法，一次性遍历的方法
var searchInsert = function (nums, target) {
  for (let i = 0; i < nums.length; i++) {
    if (target <= nums[i]) {
      return i;
    }
  }
  return nums.length;
};